﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using BOMS.Utility;
using BOMS.DBUtility;

namespace BOMS.DAL
{
    public class SysAdmin
    {

        public static int getRefreshInterval()
        {
            int interval = 0;
            string str_Sql = "select  * from sysParameter";
            DataTable dt = DBUtility.DbHelper.Rt_Table(str_Sql);
            interval = Convert.ToInt32( dt.Rows[0]["refresh"].ToString());
            return interval;
        }

        public static DataTable getParameter()
        {
            string str_Sql = "select * from sysParameter";
            DataTable dt = DBUtility.DbHelper.Rt_Table(str_Sql);
            return dt;
        }
        public static Boolean setParameter(string lanName, int refresh, decimal groupVMin, decimal groupVMax, decimal groupIMin, decimal groupIMax, decimal singleVMin, decimal singleVMax, decimal singleIMin, decimal singleIMax, decimal tempMax, string msgAlarm, string mailAlarm, string musicAlarm, string screenAlarm)
        {
              string str_Sql = "update sysParameter set lanName='" + lanName + "' ,refresh=" + refresh + ",groupVMin='" + groupVMin + "', groupVMax='" + groupVMax + "',groupIMin='" + groupIMin + "',groupIMax='" + groupIMax + "',singleVMin='" + singleVMin + "',singleVMax='" + singleVMax + "',singleIMin='" + singleIMin + "',singleIMax='" + singleIMax + "',tempMax='" + tempMax + "',msgAlarm='" + msgAlarm + "',mailAlarm='" + mailAlarm + "'" ;

              if (musicAlarm != "")
              {
                  str_Sql=str_Sql+",musicAlarm='" + musicAlarm + "'";
              }
              if (screenAlarm!="")
              {
                  str_Sql = str_Sql + ",screenAlarm='" + screenAlarm + "'";

            }
                int RtNum = DBUtility.DbHelper.ExecuteSql(str_Sql);
                if (RtNum != 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
        }
        public static Boolean updateWinceName(string id, string winceName)
        {
            string str_Sql = "update M_SubLans set HostName='" + winceName + "' where id=" + id;
            int RtNum = DBUtility.DbHelper.ExecuteSql(str_Sql);
            if (RtNum != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static Boolean deleteWince(string id)
        {
            string str_Sql = "update M_SubLans set isDel='y' where id=" + id;
            int RtNum = DBUtility.DbHelper.ExecuteSql(str_Sql);
            if (RtNum != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static DataTable getWinceOfLan()
        {

            string str_Sql = "select * from M_SubLans where isDel='n' order by regtime desc";
            DataTable dt = DBUtility.DbHelper.Rt_Table(str_Sql);
            return dt;
        }
        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="user"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static Boolean LoginCheck(string user, string pwd ,out string userid)
        {
            pwd = StringOp.HashData(pwd);
            Boolean ret = false;
            string str_Sql = "select * from sysUser where userName='" + user + "' and pwd='" + pwd + "' and isDel='n'";

            DataTable dt = DbHelper.Rt_Table(str_Sql);
            if (dt.Rows.Count > 0) //User and password are right
            {

                string userId = dt.Rows[0]["id"].ToString();
                string name = dt.Rows[0]["name"].ToString();
                string userName = dt.Rows[0]["userName"].ToString();
                // 把用户信息保存在application对象中

                ret = true;
                userid = userId;
            }
            else
            {
                userid = "";
            }
            return ret;
        }
        public static string getModuleListByUserId(string userId)
        {
            string str_Sql = "select u.id,r.modules from sysUser u,sysrole r where u.roleid=r.id and u.id=" + userId;
            DataTable dt = DBUtility.DbHelper.Rt_Table(str_Sql);
            return dt.Rows[0]["modules"].ToString();
        }
        public static DataTable getAllModules()
        {
             string str_Sql = "select * from sysModule" ;
            DataTable dt = DBUtility.DbHelper.Rt_Table(str_Sql);
            return dt;
        }
        public static Boolean deleteUser(string userId)
        {
            string str_Sql = "update sysUser set isDel='y' where id=" + userId;
            int RtNum = DBUtility.DbHelper.ExecuteSql(str_Sql);
            if (RtNum != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static Boolean resetPwd(string userId,string pwd)
        {
            pwd = StringOp.HashData(pwd);
            string str_Sql = "update sysUser set pwd='"+pwd+"' where id="+userId;
            int RtNum = DBUtility.DbHelper.ExecuteSql(str_Sql);
            if (RtNum != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static Boolean addUser(string name,string userName,string pwd,string roleId)
            {
                pwd = StringOp.HashData(pwd);
           string str_Sql="insert into sysUser(name,userName,pwd,roleId,isDel) values('"+name+"','"+userName+"','"+pwd+"',"+roleId+",'n')";
           int RtNum = DBUtility.DbHelper.ExecuteSql(str_Sql);
           if (RtNum != 0)
           {
               return true;
           }
           else
           {
               return false;
           }
            }
        public static Boolean editUser(string userId,string name, string userName, string pwd, string roleId)
        {
            pwd = StringOp.HashData(pwd);
            string str_Sql = "update sysUser set name='" + name + "',userName='" + userName + "',pwd='" + pwd + "',roleId=" + roleId + " where id="+userId;
            int RtNum = DBUtility.DbHelper.ExecuteSql(str_Sql);
            if (RtNum != 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        /// <summary>
        /// 得到所有系统用户
        /// </summary>
        /// <returns></returns>
        public static DataSet getAllUser()
        {
            string str_Sql = "select u.id,u.name,u.userName,r.role from sysUser u, sysRole r where u.roleId=r.id and u.isdel='n';; ";
            DataSet ds = DBUtility.DbHelper.Query(str_Sql);
            return ds;
        }
        public static DataTable getUserInfoById(string userId)
        {
            string str_Sql = "select * from sysUser where id="+userId;
            DataTable dt = DBUtility.DbHelper.Rt_Table(str_Sql);
            return dt;

        }
        public static DataTable getAllRole()
        {
            string str_Sql = "select id,role from sysRole; ";
            DataTable dt = DBUtility.DbHelper.Rt_Table(str_Sql);
            return dt;
        }

    }
}
